<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" >
    <title>新增页面</title>
    <link rel="stylesheet" href="../../static/layui/css/layui.css" data-th-href="@{/layui/css/layui.css}">
    <script src="../../static/layui/layui.js" data-th-src="@{/layui/layui.js}"></script>
    <script data-th-replace="public::config"></script>


</head>
<body>
<form class="layui-form" >
    <input type="hidden" name="id" id="roleId" data-th-value="${id}">
    <div class="layui-form-item">
        <label class="layui-form-label">选择权限</label>
        <div class="layui-input-block">
            <div id="LAY-auth-tree-index"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="sub">保存</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script data-th-inline="javascript">
        layui.config({
            base: '[(@{/extends/})]',
        }).extend({
            authtree: 'authtree/authtree',
        });
    //Demo
    layui.use(['form','jquery','authtree'], function(){
        let {form,$,authtree} = layui;
        //得到角色ID
        let roleId=$("#roleId").val();
        $.get(`[(@{/role/authorization})]/${roleId}`,function (rs) {
            var trees = authtree.listConvert(rs.data.list, {
                primaryKey: 'id'
                ,startPid: -1
                ,parentKey: 'pid'
                ,nameKey: 'name'
                ,valueKey: 'id'
                ,checkedKey: rs.data.checkedId
            });
            authtree.render('#LAY-auth-tree-index', trees, {
                inputname: 'resourceIds',
                layfilter: 'lay-check-auth',
                autowidth: true,
                openall: true
            });
        },'json')
        //监听提交
        form.on('submit(sub)', function(data){
            data.field.resourceIds=authtree.getAll('#LAY-auth-tree-index');
            let index = layer.load(2,{shade:0.3});
            $.ajax({
                id:'table_reload',
                url:/*[[@{/role/authorization}]]*/ '/save',
                type:'put',
                data:data.field,
                success:function (rs) {
                    layer.close(index);
                    if(rs.code == 0) {
                        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                        parent.layer.msg(rs.msg,{icon:6});
                        parent.layui.table.reload('table_reload');
                    }else {
                        parent.layer.close(index);
                        parent.layer.msg(rs.msg,{icon:5});
                    }
                }
            })

        });
    });
</script>
</body>
</html>